package de.upb.messerschmidt.set;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import de.upb.messerschmidt.set.entity.Talk;
import de.upb.messerschmidt.set.jdo.PMF;

/**
 * This Servlet is able to return {@link Talk}s regarding the supplied
 * parameters.
 * 
 * @author Tim Messerschmidt
 * 
 */
public class GetTalkServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String eventString = req.getParameter("event");
		String talkString = req.getParameter("talk");

		PersistenceManager pm = PMF.get().getPersistenceManager();

		if (eventString != null && talkString == null) {
			Query query = pm.newQuery(Talk.class);
			query.setFilter("where == whereParam");
			query.declareParameters("String whereParam");

			try {
				List<Talk> talks = (List<Talk>) query.execute(eventString);

				if (!talks.isEmpty()) {
					Gson gson = new Gson();
					String json = gson.toJson(talks);
					resp.setStatus(200);
					resp.getWriter().println(URLEncoder.encode(json, "UTF-8"));
				} else {
					resp.setStatus(200);
					resp.getWriter().println(
							"no talks for event: " + eventString + " yet");
				}
			} finally {
				query.closeAll();
				pm.close();
			}
		} else if (talkString != null && eventString == null) {
			Query query = pm.newQuery(Talk.class);
			query.setFilter("topic == topicParam");
			query.declareParameters("String topicParam");

			try {
				List<Talk> talks = (List<Talk>) query.execute(talkString);

				if (!talks.isEmpty()) {
					Gson gson = new Gson();
					String json = gson.toJson(talks.get(0));
					resp.setStatus(200);
					resp.getWriter().println(URLEncoder.encode(json, "UTF-8"));
				} else {
					resp.setStatus(200);
					resp.getWriter().println(
							"no talks with topic: " + talkString + " yet");
				}
			} finally {
				query.closeAll();
				pm.close();
			}
		} else if (talkString != null && eventString != null) {
			Query query = pm.newQuery(Talk.class);
			query.setFilter("topic == topicParam && where == eventParam");
			query.declareParameters("String topicParam, String eventParam");

			try {
				List<Talk> talks = (List<Talk>) query.execute(talkString,
						eventString);

				if (!talks.isEmpty()) {
					Gson gson = new Gson();
					String json = gson.toJson(talks.get(0));
					resp.setStatus(200);
					resp.getWriter().println(URLEncoder.encode(json, "UTF-8"));
				} else {
					resp.setStatus(200);
					resp.getWriter().println(
							"no talks with topic: " + talkString + " yet");
				}
			} finally {
				query.closeAll();
				pm.close();
			}
		} else {
			Query query = pm.newQuery(Talk.class);

			try {
				List<Talk> talks = (List<Talk>) query.execute();

				if (!talks.isEmpty()) {
					Gson gson = new Gson();
					String json = gson.toJson(talks);
					resp.setStatus(200);
					resp.getWriter().println(URLEncoder.encode(json, "UTF-8"));
				} else {
					resp.setStatus(200);
					resp.getWriter().println(
							"no talks for event: " + eventString + " yet");
				}
			} finally {
				query.closeAll();
				pm.close();
			}
		}
	}

}
